#ifndef __HDVB_CHANSDB_H
#define __HDVB_CHANSDB_H
/*
    libhdvbd - Library for building sophisticated DVB applications
    Copyright (C) 2012 Tony Houghton <h@realh.co.uk>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free dSoftware
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

/* Channels database */

#include <glib.h>

#include <sqlite3.h>

#include <hdvb/dbcommon.h>

G_BEGIN_DECLS

#define HDVB_CHANSDB_VERSION 1

/* Underlying structure and queries/statements */

enum {
    HDVB_CHANSDB_COLUMN_TRANSPORTS_TS_ID = 0,
    HDVB_CHANSDB_COLUMN_TRANSPORTS_NETWORK_ID,
    HDVB_CHANSDB_COLUMN_TRANSPORTS_TUNING_STR,
    
    HDVB_CHANSDB_TRANSPORTS_N_COLUMNS
};

enum {
    HDVB_CHANSDB_COLUMN_SERVICES_SERVICE_ID = 0,
    HDVB_CHANSDB_COLUMN_SERVICES_TS_ID,
    HDVB_CHANSDB_COLUMN_SERVICES_FREE_CA_MODE,
    HDVB_CHANSDB_COLUMN_SERVICES_SERVICE_TYPE,
    HDVB_CHANSDB_COLUMN_SERVICES_LCN,
    HDVB_CHANSDB_COLUMN_SERVICES_SERVICE_NAME,
    HDVB_CHANSDB_COLUMN_SERVICES_PROVIDER_NAME,
    
    HDVB_CHANSDB_SERVICES_N_COLUMNS
};

void
hdvb_chans_db_create(sqlite3 *sql, gpointer handle);

/* creator may be NULL to use default */
sqlite3 *
hdvb_chans_db_open(const char *filename, HdvbDbCreator creator,
        gboolean with_pids);

gboolean
hdvb_chans_db_get_is_valid(sqlite3 *sql);

void
hdvb_chans_db_set_is_valid(sqlite3 *sql, gboolean valid);

sqlite3_stmt *
hdvb_chans_db_statement_insert_service(sqlite3 *sql);

sqlite3_stmt *
hdvb_chans_db_statement_insert_transport(sqlite3 *sql);

sqlite3_stmt *
hdvb_chans_db_statement_insert_lcn(sqlite3 *sql);

sqlite3_stmt *
hdvb_chans_db_statement_insert_dvbt2_ids(sqlite3 *sql);

#if 0
sqlite3_stmt *
hdvb_chans_db_statement_insert_service_region(sqlite3 *sql);

sqlite3_stmt *
hdvb_chans_db_statement_insert_region_name(sqlite3 *sql);
#endif

/* Result row contains all fields from services followed by all fields
 * from transports.
 */
sqlite3_stmt *
hdvb_chans_db_statement_lookup_service_by_name(sqlite3 *sql);

#define HDVB_CHANS_DB_TYPE_ALL 0
#define HDVB_CHANS_DB_TYPE_SDTV 1
#define HDVB_CHANS_DB_TYPE_HDTV 2
#define HDVB_CHANS_DB_TYPE_RADIO 4
#define HDVB_CHANS_DB_TYPE_TELETEXT 8

/* Returns (lcn, service_id, service_name).
 * order_by should be NULL or something like "lcn", "service_name"; be careful
 * not to let it be abused eg for injection attacks.
 * type_mask is ORred from HDVB_CHANS_DB_TYPE values above.
 */
sqlite3_stmt *
hdvb_chans_db_statement_get_services_brief(sqlite3 *sql,
        const char *order_by, gboolean include_scrambled, guint type_mask);

/* Returns (lcn, service_id, service_name, ts_id, tuning_str) */
sqlite3_stmt *
hdvb_chans_db_statement_get_services_with_tuning(sqlite3 *sql,
        const char *order_by, gboolean include_scrambled, guint type_mask);

/* Returns a tuning_str for a bound ts_id */
sqlite3_stmt *
hdvb_chans_db_statement_lookup_tuning_for_ts_id(sqlite3 *sql);

/* Returns (ts_id, tuning_str) for a bound service_id */
sqlite3_stmt *
hdvb_chans_db_statement_lookup_tuning_for_service_id(sqlite3 *sql);

/* Updates tuning_str to param 1 for ts_id param 2 */
sqlite3_stmt *
hdvb_chans_db_statement_update_tuning_for_ts_id(sqlite3 *sql);

G_END_DECLS

#endif /* __HDVB_CHANSDB_H */
